package 字符串;

public class addStrings {
    public static String addStrings(String num1, String num2) {
        int len1=num1.length(),len2=num2.length();
        int r1=len1-1,r2=len2-1;
        StringBuilder builder = new StringBuilder();
        // 进位
        int add=0,val=0;
        char c1 = '0',c2='0';
        while (r1>=0 || r2>=0){
            if(r1<0){
                c1 = '0';
                c2 = num1.charAt(r2);
            }
            if(r2<0){
                c1 = num1.charAt(r1);
                c2='0';
            }
            if(r1>=0 && r2>=0){
                c1 = num1.charAt(r1);
                c2 = num2.charAt(r2);
            }

            val=(c1-'0'+c2-'0'+add);
            add=val/10;
            builder.append(String.valueOf(val%10));
            r1--;
            r2--;
        }
        if(add!=0) builder.append(String.valueOf(add));

        return builder.reverse().toString();
    }

    public static void main(String[] args) {
        System.out.println(addStrings("456","77"));
    }
}
